<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>
<%@page import="com.shsh.safemanage.model.HSECategory"%>
<%@page import="com.shsh.safemanage.action.UserAction"%>
<%@page import="com.shsh.safemanage.model.Department1"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.EquipmentDao"%>
<%@page import="com.shsh.safemanage.model.Equipment"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.util.Constants"%>

<div class="hider" id="hider">
	<span>&nbsp;</span>
</div>
<div class="position">
	<img src="images/position2.jpg" style="padding: 7px 9px; float: left;" />当前位置：上海高桥分公司问题管理平台 &gt;&gt; 通用问题填报
</div>
<%
User theLoginUser = (User) session.getAttribute(Constants.SESSION_USER);

session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
session.removeAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);
session.removeAttribute(Constants.SESSION_PROBLEM_EQUIPMENT);

WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();
UserAction userAction = ActionCreator.createUserAction();
Role role = userAction.getUserRole(request);
int intRole = writeProblemAction.calRole(null, null, null, role);
%>
<div class="tt">问题填报卡</div>
<div class="content">
	<table align="center" border="1" width="900" cellpadding="0"
		cellspacing="0">
		<tr>
			<td class="td1" height="35" id="chooseDepartEquipTD">
				<%@include file="write_problem_depart_equip_section.jsp" %>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35" valign="middle">
				<span style="margin-left: 40px" class="span1">发现问题标题：（即：填写HSE观察作业卡上的“区域/设施”+“观察的作业”）</span>
				<br />
				<input type="text" style="margin-left: 60px" id="problemTitle" size="45" />
				<input type="checkbox" id="isSolvedCheck" style="margin-left: 10px" onclick="checkSolvedElement()" />
				<label for="isSolvedCheck" style="margin-left: 3px">观察正常</label>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">发现问题内容：（即：HSE观察作业卡上的“观察事实描述”，多人同组进行HSE观察，请务必在如下文本框第一行中填写姓名，若仅本人观察，可删除第一行）</span>
				<br />
				<textarea style="width: 600px; height: 100px; margin-left: 60px" id="problemContent">本次观察人还包括___和___。</textarea>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">整改措施：（即：HSE观察作业卡上的“立即采取的措施”）</span>
				<br />
				<textarea style="width: 600px; height: 100px; margin-left: 60px" id="zhenggai_cuoshi"></textarea>
			</td>
		</tr>
		<tr id="hseCategoryTR">
			<td class="td1" height="35">
				<div id="hseCategoryDiv">
				<%@include file="problem_hse_category_section.jsp" %>
				</div>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<%
				if ((intRole & (Constants.ROLE_TYPE_ZHUANGZHIZHANG)) == 0) {
				%>
				<div id="informPersonDiv">
				<%@include file="write_problem_informed_person.jsp" %>
				</div>
				<%
				}
				%>
				<div id="chooseManageGroupUserDiv">
				<%@include file="managegroup_select/manage_check_samepage_section.jsp" %>
				</div>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">填报人及时间：</span>
				<span><%=theLoginUser.getDepartment1Name()%>-<%=theLoginUser.getDepartment2Name()%>-<%=theLoginUser.getDepartment3Name()%></span>
				<span><%=theLoginUser.getName()%></span>
				<span><%=Util.formatDateTime(Calendar.getInstance())%></span>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<input type="button" style="margin-left: 300px" value="提 交" onclick="writeProblem()" />
				<input type="button" style="margin-left: 30px" value="取 消" onclick="cancelProblem()" />
			</td>
		</tr>
	</table>
</div>

<script type="text/javascript">
function writeProblem() {
	if (getElementValueByID('problemTitle') == '') {
		$.dialog.alert('请输入标题');
		return;
	}
	if (getElementValueByID('problemContent').length <= 5) {
		$.dialog.alert('在发现问题内容一栏里必需填报事实描述');
		return;
	}
	if (getElementValueByID('zhenggai_cuoshi') == '') {
		$.dialog.alert('请输入整改措施');
		return;
	}

	if (document.getElementById('equipID') != null) {
		if (getElementValueByID('equipID') == '') {
			$.dialog.alert('请选择发生问题的装置');
			return;
		}
	}
	
	var userIDs = '';
	var manageGroupUserCheckEles = document.getElementsByName('manageGroupUserCheck');
	for (var i = 0; i != manageGroupUserCheckEles.length; i++) {
		if (manageGroupUserCheckEles[i].checked) {
			if (userIDs == '') {
				userIDs = userIDs + manageGroupUserCheckEles[i].value;
			} else {
				userIDs = userIDs + ',' + manageGroupUserCheckEles[i].value;
			}
		}
	}
	
	var categoryPerPosID = '';
	var categoryPerPosCheckEles = document.getElementsByName('categoryPerPos');
	for (var i = 0; i != categoryPerPosCheckEles.length; i++) {
		if (categoryPerPosCheckEles[i].checked) {
			if (categoryPerPosID == '') {
				categoryPerPosID = categoryPerPosCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个人员位置');
				return;
			}
		}
	}
	
	var categoryPerEquipID = '';
	var categoryPerEquipCheckEles = document.getElementsByName('categoryPerEquip');
	for (var i = 0; i != categoryPerEquipCheckEles.length; i++) {
		if (categoryPerEquipCheckEles[i].checked) {
			if (categoryPerEquipID == '') {
				categoryPerEquipID = categoryPerEquipCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个装备配合或使用不当');
				return;
			}
		}
	}
	
	var categoryWorkActID = '';
	var categoryWorkActCheckEles = document.getElementsByName('categoryWorkAct');
	for (var i = 0; i != categoryWorkActCheckEles.length; i++) {
		if (categoryWorkActCheckEles[i].checked) {
			if (categoryWorkActID == '') {
				categoryWorkActID = categoryWorkActCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个作业行为');
				return;
			}
		}
	}
	
	var categoryToolEquipID = '';
	var categoryToolEquipCheckEles = document.getElementsByName('categoryToolEquip');
	for (var i = 0; i != categoryToolEquipCheckEles.length; i++) {
		if (categoryToolEquipCheckEles[i].checked) {
			if (categoryToolEquipID == '') {
				categoryToolEquipID = categoryToolEquipCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个工具及设备');
				return;
			}
		}
	}
	
	var categoryWorkEnvID = '';
	var categoryWorkEnvCheckEles = document.getElementsByName('categoryWorkEnv');
	for (var i = 0; i != categoryWorkEnvCheckEles.length; i++) {
		if (categoryWorkEnvCheckEles[i].checked) {
			if (categoryWorkEnvID == '') {
				categoryWorkEnvID = categoryWorkEnvCheckEles[i].value;
			} else {
				$.dialog.alert('请仅选择一个作业场所与环境');
				return;
			}
		}
	}
	
	var isSolved = false;
	if (document.getElementById('isSolvedCheck').checked) {
		isSolved = true;
	}
	
	if (!document.getElementById('isSolvedCheck').checked) {
		if (categoryPerPosID == '' && categoryPerEquipID == '' && categoryWorkActID == '' 
				&& categoryToolEquipID == '' && categoryWorkEnvID == '') {
			$.dialog.alert('请选择问题分类');
			return;
		}
	}
	
	if ($.dialog.confirm('您确定提交此问题吗？', function(){
		openWaitingDialog('waitingDialog');
		$.ajax({
			type:'post',
			url:'write_problem_process.jsp',
			data:{
				problemTitle: getElementValueByID('problemTitle'),
				problemContent: getElementValueByID('problemContent'),
				zhenggai_cuoshi: getElementValueByID('zhenggai_cuoshi'),
				equipID: getElementValueByID('equipID'),
				userID: userIDs,
				depart1ID: getElementValueByID('depart1'),
				isSolved: isSolved,
				categoryPerPosID: categoryPerPosID,
				categoryPerEquipID: categoryPerEquipID,
				categoryWorkActID: categoryWorkActID,
				categoryToolEquipID: categoryToolEquipID,
				categoryWorkEnvID: categoryWorkEnvID,
				categoryPerPosOther: getElementValueByID('categoryPerPosOther'),
				categoryPerEquipOther: getElementValueByID('categoryPerEquipOther'),
				categoryWorkActOther: getElementValueByID('categoryWorkActOther'),
				categoryToolEquipOther: getElementValueByID('categoryToolEquipOther'),
				categoryWorkEnvOther: getElementValueByID('categoryWorkEnvOther')
			},
			success:function(data,state,xmlhttp){
				var obj = null;
				try {
					obj = eval('(' + data + ')');
				} catch (e) {
				}
				if (typeof(obj.isSuccess) != 'undefined') {
					closeWaitingDialog('waitingDialog');
					if (obj.isSuccess == 'true') {
						$.dialog('问题填报成功！', function() { loadPageSection('<%=request.getContextPath()%>/user_infor.jsp', 'mainContentDiv'); });
					} else {
						$.dialog.alert(obj.error);
					}
				}
			}
		});
	}, function(){}));
}

function cancelProblem() {
	if ($.dialog.confirm('您确定取消此问题吗？', function(){
		loadPageSection('<%=request.getContextPath()%>/user_infor.jsp', 'mainContentDiv');
	}, function(){}));
}

function changeEquip() {
	var equipID = getElementValueByID('equipID');
	loadPageSection('write_problem_informed_person.jsp', 'informPersonDiv', {equipID: equipID}, false);
}

function checkSolvedElement() {
	var isSolvedElement = document.getElementById('isSolvedCheck');
	if (isSolvedElement.checked) {
		setElementValueByID('problemContent', '经观察正常。');
		setElementValueByID('zhenggai_cuoshi', '观察正常');
		setDisplay('hseCategoryTR', false);
	} else {
		setElementValueByID('problemContent', '本次观察人还包括___和___。');
		setElementValueByID('zhenggai_cuoshi', '');
		setDisplay('hseCategoryTR', true);
	}
}

<%
if ((intRole & Constants.ROLE_TYPE_ZUYUAN | Constants.ROLE_TYPE_KEYUAN
		| Constants.ROLE_TYPE_FUKEZHANG | Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUCHUZHANG
		| Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_ZONGSHI | Constants.ROLE_TYPE_FUZONGJINGLI
		| Constants.ROLE_TYPE_ZONGJINGLI) > 0) {
%>
changeEquip();
<%
}
%>
</script>